package first.group.ssh.globalstate;

import java.io.IOException;
import java.nio.ByteBuffer;

public abstract class GlobalState {

    public abstract String getName();

    public abstract RequestPacket.Parser<? extends GlobalState> getRequestParser();

    public abstract SuccessPacket.Parser<? extends GlobalState> getSuccessParser();

    public static abstract class RequestPacket<T extends GlobalState> {

        public final boolean wantReply;

        public RequestPacket(boolean wantReply) {
            this.wantReply = wantReply;
        }

        public abstract String getName();

        public abstract T getState();

        public abstract void write(ByteBuffer buffer);

        public static abstract class Parser<T extends GlobalState> {

            public abstract RequestPacket<T> parse(boolean wantReply, ByteBuffer buffer) throws IOException;
        }
    }

    public static abstract class SuccessPacket<T extends GlobalState> {

        public abstract boolean getWantReply();

        public abstract String getName();

        public abstract T getState();

        public abstract void write(ByteBuffer buffer);

        public static abstract class Parser<T extends GlobalState> {

            public RequestPacket<T> parse(boolean wantReply, byte[] payload) throws IOException {
                return parse(wantReply, ByteBuffer.wrap(payload));
            }

            public RequestPacket<T> parse(boolean wantReply, ByteBuffer payloadBuffer) throws IOException {
                throw new UnsupportedOperationException("No parse method implemented.");
            }
        }
    }
}
